package com.doitedu.mr.day04.json;

import com.doitedu.mr.day03.User;
import com.google.gson.Gson;

/**
 * @Date 2021/12/3
 * @Created by HANGGE
 * @Description
 * Gson
 * java 类转换成json字符串
 * json字符串转换成 JAVA类
 */
public class TestJson {
    public static void main(String[] args) {
        // JavaBean  -->  json串
        User user = new User(1, "柳岩", 41);
        // JSON解析工具
        Gson gson = new Gson();
        String str = gson.toJson(user);
        System.out.println(str);//{"id":1,"name":"柳岩","age":41}

        // 将json串转换成java对象
        User user1 = gson.fromJson(str, User.class);
        System.out.println(user1);
    }
}
